
	  
using Youz.Common.Entity;
using Youz.Common.Helper;
using Youz.CreditRepay.BLL;
using Youz.CreditRepay.Entity;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;
using Youz.Common.BLL;

namespace Youz.CreditRepay.Manage.Handlers
{
    public partial class Pay_QRCodeHandler : IHttpHandler, IRequiresSessionState
    {
		Pay_QRCodeBLL bll = new Pay_QRCodeBLL();
		
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
			
            string _type = WebHelper.Get("type");
            string result = string.Empty;
            switch (_type)
            {
                case "get":
                    result = Get();
                    break;  
                case "list":
                    result = List();
                    break;
                case "save":
                    result = Save();
                    break;
                case "delete":
                    result = Delete();
                    break;
                case "GetAmountList":
                    result = GetAmountList();
                    break;
            }
			
            context.Response.Write(result);
            context.Response.End();
        }

        private string GetAmountList()
        {
            DataTable dt = CommonHelper.GetDateTable(new Pay_AmountBLL().Gets(string.Format(" PayType={0} ", WebHelper.Post("t"))));

            return Core.GetJsonByList(dt);
        }
		
        private string Get()
        {
            Int32 ID = WebHelper.Post<Int32>("ID");
            
            return bll.GetJson(ID);
        }

        private string List()
        {
            int pageIndex = WebHelper.Post<int>("start");
            int pageSize = WebHelper.Post<int>("length");

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat(" PayType={0} ", WebHelper.Post("PayType"));
            //sb.Append(CommonHelper.GetDateQuerySql(WebHelper.Post("StartDate"), WebHelper.Post("EndDate"), "ApplyTime"));

			int total = 0;
            DataTable dt = bll.Gets(sb.ToString(), pageIndex, pageSize, out total);

            return Core.GetJsonByDataTableHaveTime(total, dt);
        }

        private string Save()
        {
            string id = WebHelper.Post("ID");
            bool isAdd = string.IsNullOrWhiteSpace(id);          
            Pay_QRCode model = new Pay_QRCode();            

            if (isAdd)
            {
            }
            else
            {
                model = bll.Get(WebHelper.Post<Int32>("ID"));
            }

            model.Name = WebHelper.Post<String>("Name");
			model.AmountID = WebHelper.Post<Int32>("AmountID");
			model.Img = WebHelper.Post<String>("Img");
			model.Remark = WebHelper.Post<String>("Remark");

            if (WebHelper.HasFiles("_Img"))
            {
                var img = string.Empty;
                var uploadReturn = Utility.UploadImage("_Img", "PayQR", out img);

                if (string.IsNullOrEmpty(uploadReturn))
                {
                    model.Img = string.IsNullOrEmpty(img) ? model.Img : img;
                }
                else
                {
                    return uploadReturn;
                }
            }

            if (isAdd ? bll.Add(model) : bll.Update(model))
            {
                return Core.AjaxResult(true, AjaxResultType.Save);
            }
            else
            {
                return Core.AjaxResult(false, AjaxResultType.Save);
            }
        }
		
        private string Delete() 
        {
            if (bll.Delete(WebHelper.Post<Int32>("ID")))
            {
                return Core.AjaxResult(true, AjaxResultType.Delete);
            }
            else
            {
                return Core.AjaxResult(false, AjaxResultType.Delete);
            }
        }
		
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

  